// LineChartView.java
package com.example.xinli.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class LineChartView extends View {
    private Paint gridPaint;
    private Paint linePaint;
    private Paint pointPaint;
    private Paint textPaint;
    private List<Integer> dataPoints;
    private List<String> labels;
    private int maxValue = 10000; // 默认最大值

    public LineChartView(Context context) {
        super(context);
        init();
    }

    public LineChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 网格画笔
        gridPaint = new Paint();
        gridPaint.setColor(Color.GRAY);
        gridPaint.setStrokeWidth(2f);
        gridPaint.setAlpha(100);

        // 折线画笔
        linePaint = new Paint();
        linePaint.setColor(Color.WHITE);
        linePaint.setStrokeWidth(5f);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);

        // 数据点画笔
        pointPaint = new Paint();
        pointPaint.setColor(Color.WHITE);
        pointPaint.setStrokeWidth(8f);
        pointPaint.setStyle(Paint.Style.FILL);

        // 文字画笔
        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(30f);
        textPaint.setAntiAlias(true);

        dataPoints = new ArrayList<>();
        labels = new ArrayList<>();
    }

    public void setData(List<Integer> dataPoints, List<String> labels) {
        this.dataPoints = dataPoints;
        this.labels = labels;
        invalidate(); // 重绘视图
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (dataPoints == null || dataPoints.isEmpty()) {
            return;
        }

        int width = getWidth();
        int height = getHeight();
        int paddingLeft = 60;
        int paddingRight = 30;
        int paddingTop = 30;
        int paddingBottom = 80;

        int chartWidth = width - paddingLeft - paddingRight;
        int chartHeight = height - paddingTop - paddingBottom;

        // 绘制网格线和Y轴标签
        int gridLines = 5;
        for (int i = 0; i <= gridLines; i++) {
            float y = paddingTop + (chartHeight * i / gridLines);
            canvas.drawLine(paddingLeft, y, width - paddingRight, y, gridPaint);

            // 绘制Y轴标签
            int value = maxValue - (i * maxValue / gridLines);
            canvas.drawText(String.valueOf(value), 10, y + 10, textPaint);
        }

        // 计算数据点位置
        List<PointF> points = new ArrayList<>();
        float xStep = (float) chartWidth / (dataPoints.size() - 1);

        for (int i = 0; i < dataPoints.size(); i++) {
            float x = paddingLeft + i * xStep;
            float y = paddingTop + chartHeight - (chartHeight * dataPoints.get(i) / maxValue);
            points.add(new PointF(x, y));
        }

        // 绘制折线
        if (points.size() > 1) {
            Path path = new Path();
            path.moveTo(points.get(0).x, points.get(0).y);

            for (int i = 1; i < points.size(); i++) {
                path.lineTo(points.get(i).x, points.get(i).y);
            }

            canvas.drawPath(path, linePaint);
        }

        // 绘制数据点和X轴标签
        for (int i = 0; i < points.size(); i++) {
            PointF point = points.get(i);
            canvas.drawCircle(point.x, point.y, 8f, pointPaint);

            // 绘制X轴标签
            if (i < labels.size()) {
                String label = labels.get(i);
                float textWidth = textPaint.measureText(label);
                canvas.drawText(label, point.x - textWidth / 2, height - 20, textPaint);
            }
        }
    }
}
